package com.moorecat.app;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.moorecat.R;
import com.moorecat.utils.ui.UiUtils;

public class ActivityCommentAdapter extends BaseAdapter {

	ActivityDetail ctx;

	List<com.moorecat.bean.Comment> list;

	private List<Integer> selectedItems = new ArrayList<Integer>();

	public ActivityCommentAdapter(ActivityDetail ctx,
			List<com.moorecat.bean.Comment> list) {
		this.ctx = ctx;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final com.moorecat.bean.Comment comment = list.get(position);
		View itemView = convertView;
		if (itemView == null) {
			itemView = LayoutInflater.from(ctx).inflate(
					R.layout.activity_comment_item, null);
		}
		ImageView pic = (ImageView) itemView.findViewById(R.id.pic);
		TextView name = (TextView) itemView.findViewById(R.id.name);
		final TextView content = (TextView) itemView
				.findViewById(R.id.comment_content);
		TextView date = (TextView) itemView.findViewById(R.id.comment_date);
		final TextView more = (TextView) itemView.findViewById(R.id.more);

		pic.setImageBitmap(comment.getUser().getIcon());
		name.setText(comment.getUser().getUserName());
		content.setText(comment.getContent());
		date.setText(comment.getDate());
		if (comment.getContent().length() < 54) {
			if (!selectedItems.contains(position)) {
				more.setVisibility(View.GONE);
			}
		} else {
			more.setVisibility(View.VISIBLE);
		}
		itemView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (comment.getContent().length() > 54) {
					if (selectedItems.contains(position)) {
						more.setVisibility(View.VISIBLE);
						content.setMaxLines(3);
						selectedItems.remove((Integer) position);
					} else {
						more.setVisibility(View.GONE);
						content.setMaxLines(100);
						selectedItems.add(position);
					}
				}
                //ActivityCommentAdapter.this.notifyDataSetChanged();
                UiUtils.setListViewHeightBasedOnChildren(ctx.getListView());
			}
		});
		return itemView;
	}

}
